package com.ciaojian.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ciaojian.core.model.Scheduling;
import com.ciaojian.core.model.SchedulingExamine;
import com.ciaojian.core.model.dto.SchedulingDTO;
import com.ciaojian.core.model.vo.RosterWorkerTimeVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 排班表 Mapper 接口
 * </p>
 *
 * @author Atlakyn
 * @since 2021-03-25
 */
public interface SchedulingMapper extends BaseMapper<Scheduling> {
    /**
     * 查询发型师未来三天的排班
     *
     * @param workerId 发型师ID
     * @return 返回日期、上班时间和结束时间等
     */
    @MapKey("id")
    List<Map<String, Object>> findFutureSchedule(@Param("workerId") Integer workerId);

    /**
     * 根据发型师查询当天排班信息
     *
     * @param workerId 发型师 Id
     * @return 排班信息
     */
    @Select("SELECT * FROM d_scheduling WHERE worker_id = #{workerId} " +
            "AND date = CURDATE() " +
            "AND logic_del = FALSE " +
            "AND status = 1 ")
    @Results({
            @Result(column = "roster_id", property = "roster", one = @One(select = "com.ciaojian.core.mapper.RosterMapper.getById"))
    })
    SchedulingDTO findByWorkerAndCurDate(@Param("workerId") Integer workerId);

    /**
     * 查询门店的排班信息,按时间范围
     *
     * @param storeId   门店Id
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return 排班信息
     */
    @Select("SELECT * FROM d_scheduling WHERE store_id = #{storeId}  " +
            "AND date BETWEEN #{beginDate} AND #{endDate}  " +
            "AND status = 1 " +
            "AND logic_del = FALSE")
    @Results({
            @Result(column = "roster_id", property = "roster", one = @One(select = "com.ciaojian.core.mapper.RosterMapper.getById"))
    })
    List<SchedulingDTO> findByStoreBetween(@Param("storeId") Integer storeId,
                                           @Param("beginDate") Date beginDate,
                                           @Param("endDate") Date endDate);

    /**
     * 根据发型师和日期查询排班信息
     *
     * @param workerId 发型师 Id
     * @param day      日期
     * @return 排班信息
     */
    @Select("SELECT * FROM d_scheduling WHERE worker_id = #{workerId} " +
            "AND date = DATE_FORMAT(#{day}, '%Y-%m-%d') " +
            "AND status = 1 " +
            "AND logic_del = FALSE")
    @Results({
            @Result(column = "roster_id", property = "roster", one = @One(select = "com.ciaojian.core.mapper.RosterMapper.getById"))
    })
    SchedulingDTO findByWorkerAndDate(@Param("workerId") Integer workerId, @Param("day") Date day);


    /**
     * 根据排班申请记录Id查询发型师排班信息
     *
     * @param examineId 排班申请ID
     * @return 发型师排班信息
     */
    @Select("SELECT * FROM d_scheduling WHERE examine_id = #{examineId} ORDER BY date")
    @Results({
            @Result(column = "roster_id", property = "roster", one = @One(select = "com.ciaojian.core.mapper.RosterMapper.getById"))
    })
    List<SchedulingDTO> findByExamineId(Integer examineId);


    /**
     * 查询发型师排班日历
     *
     * @param workerId 发型师ID
     * @param month    月份
     * @return 发型师排班信息
     */
    List<SchedulingDTO> findCalendarByWorker(@Param("workerId") Integer workerId, @Param("month") Date month);

    RosterWorkerTimeVo getRosterWorkerTime(@Param("workerId") int workerId, @Param("date") String date);

    int getLeaveDays(@Param("workerId") int workerId, @Param("date") String date);

    Integer getWorkerRosterId(@Param("id") Integer id);

}
